<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
        <Modal v-model="showModal" :closable='false' :mask-closable=true :width="1000">
            <h3 slot="header" style="color:#2D8CF0">选择产品</h3>
            <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                    可选择产品
                </p>
                <product-list :status="status" ref="productList" v-on="{select:select}"></product-list>
            </Card>
            <Card :bordered="false" :dis-hover="true">
                <p slot="title">
                    已选择产品
                </p>
                <Table border :columns="columns1" :data="data1"></Table>
            </Card>
            <div slot="footer">
                <Button type="info" size="large" @click="confirm">确认选择</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    import productList from '@/view/basic/info/list/ProductList.vue';
    export default {
        components: {
            iviewTable,
            iviewPage,
            productList
        },
        props: {
            homeId: {
                type: Number,
                default: -1
            },
            planType: {
                type: Number,
                default: 0
            }
        },
        data () {
            return {
                baseURL: '/user/home/product',
                selectRows: [],
                data1: [],
                columns1: [
                    {
                        title: '大类',
                        key: 'bigClassName',
                        width: 150
                    },
                    {
                        title: '小类',
                        key: 'smallClassName',
                        width: 150
                    },
                    {
                        title: '产品名称',
                        key: 'name'
                    },
                    {
                        title: '数量',
                        key: 'quantity',
                        width: 150,
                        render: (h, params) => {
                            var vm = this
                            return h('InputNumber',{
                                props: {
                                    min: 1,
                                    value: params.row.quantity
                                },
                                style: {
                                    width: '100%'
                                },
                                on: {
                                    'on-change' (value) {
                                        params.row.quantity = value
                                        vm.data1[params.index] = params.row
                                    }
                                }
                            });
                        }
                    },
                    {
                        title: '操作',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'default'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/home/product',
                    defaultFilters: [
                        {'field': 'home_id', 'value1': -1, 'operate': '=='},
                        {'field': 'plan_type', 'value1': 0, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 2655,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },{
                            title: '产品名称',
                            key: 'productName'
                        },{
                            title: '数量',
                            key: 'quantity'
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                },
                showModal: false,
                status: false
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'add') {
                    this.add()
                } else if (path === 'delete') {
                    this.delete()
                }
            },
            add () {
                this.data1 = []
                this.status = !this.status
                this.showModal = true
            },
            delete () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    if (ids.length === 1) {
                        this.doDelete(ids)
                    } else {
                        this.$Modal.confirm({
                            title: '操作提示',
                            content: '<p>确认删除'+ids.length+'条记录?</p>',
                            onOk: () => {
                                this.doDelete(ids)
                            }
                        })
                    }
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = this.baseURL + '/delete'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', msg)
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                });
            },
            select (row) {
                row.homeId = this.homeId
                row.planType = this.planType
                row.productId = row.id
                row.productName = row.name
                row.quantity = 1
                this.data1.push(row)
            },
            remove (index) {
                this.data1.splice(index, 1);
            },
            confirm () {
                if (this.data1.length > 0) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.data1).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '操作成功')
                            this.showModal = false
                            this.data1 = []
                            this.refresh()
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            refresh () {
                if (this.homeId && this.homeId !== '') {
                    this.queryModel.defaultFilters[0] = {'field': 'home_id', 'value1': this.homeId, 'operate': '=='}
                }
                if (this.planType && this.planType !== '') {
                    this.queryModel.defaultFilters[1] = {'field': 'plan_type', 'value1': this.planType, 'operate': '=='}
                }
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
            }
        }
    }
</script>

<style>

</style>
